package com.study.javaBasic.reflection;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class FieldInfo {
    public static void main(String[] args) throws Exception {
        Class stdClass = Student.class;
        // 获取public字段"score":
        System.out.println(stdClass.getField("score"));
        // 获取继承的public字段"name":
        System.out.println(stdClass.getField("name"));
        // 获取private字段"grade":
        System.out.println(stdClass.getDeclaredField("grade"));
        System.out.println(stdClass.getDeclaredFields());
        System.out.println("------------------------");
        Field f = String.class.getDeclaredField("value");
        // 获取属性名称"value"
        System.out.println(f.getName());
        // 获取属性类型class [B 表示byte[]类型
        System.out.println(f.getType());
        //以整数形式返回由此 Field 对象表示的字段的 Java 语言修饰符。
        int m = f.getModifiers();
        System.out.println(m);
        System.out.println(Modifier.isFinal(m));
        System.out.println(Modifier.isPublic(m));
        System.out.println(Modifier.isProtected(m));
        System.out.println(Modifier.isPrivate(m));
        System.out.println(Modifier.isStatic(m));
        Object p = new Person("Xiao Ming");
        Class c = p.getClass();
        Field fp = c.getDeclaredField("name");
        // 获取属性值:"Xiao Ming"
        System.out.println(fp.get(p));
        fp.setAccessible(true);
        fp.set(p, "修改后的值");
        System.out.println(fp.get(p));
    }
}

class Student extends Person {
    public int score;
    private int grade;

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public int getInfo(int grade) {
        return grade + 1;
    }
}

class Person {
    public String name;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
